#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    ui->soundQua->addItem(tr("48kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("44.1kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("16kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("8kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("48kHz, 16bit, 1channels"));
    ui->soundQua->addItem(tr("44.1kHz, 16bit, 1channels"));
    ui->soundQua->addItem(tr("16kHz, 16bit, 1channels"));
    ui->soundQua->addItem(tr("8kHz, 16bit, 1channels"));

    resizeUi.init(400,450);
    resizeUi.addInfo(ui->info);
    resizeUi.addInfo(ui->input);
    resizeUi.addInfo(ui->label);
    resizeUi.addInfo(ui->pushButton);
    resizeUi.addInfo(ui->pushButton_2);
    resizeUi.addInfo(ui->pushButton_3);
    resizeUi.addInfo(ui->pushButton_4);
    resizeUi.addInfo(ui->pushButton_5);
    resizeUi.addInfo(ui->pushButton_6);
    resizeUi.addInfo(ui->pushButton_7);
    resizeUi.addInfo(ui->sInputDev);
    resizeUi.addInfo(ui->soundQua);


    clientSend=new SendAudio();
    soundServer=new SoundServer();
    tcpSocket = new QTcpSocket(this);

    init();
    setVisibles(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setVisibles(bool v)
{
    ui->info->setVisible(true);
    ui->input->setVisible(!v);
    ui->label->setVisible(v);
    ui->pushButton->setVisible(!v);
    ui->pushButton_2->setVisible(v);
    ui->pushButton_3->setVisible(v);
    ui->pushButton_4->setVisible(v);
    ui->pushButton_5->setVisible(v);
    ui->pushButton_6->setVisible(v);
    ui->pushButton_7->setVisible(v);
    ui->sInputDev->setVisible(v);
    ui->soundQua->setVisible(v);
}

void MainWindow::init()
{
    QList<QHostAddress> addr = QNetworkInterface::allAddresses();
    ui->info->appendPlainText(tr("IP Address"));
    for(int i=0;i<addr.size();i++)
        ui->info->appendPlainText(addr.at(i).toString());

    connect(tcpSocket,&QTcpSocket::connected,[=](){
        ui->info->appendPlainText(tr("connect server!"));
        char temp[100]="Device";
        tcpSocket->write(temp,10);
    });

    connect(tcpSocket,&QTcpSocket::readyRead,[=](){
        QByteArray buf = tcpSocket->readAll();

        int start=0;
        int index=0;
        char *data=buf.data();
        while(1){
            index=buf.indexOf("#*#",start);
            if(index==-1)
            {
                QByteArray temp(&data[start],buf.size()-start);
                recvMess(temp);
                break;
            }
            else
            {
                QByteArray temp(&data[start],index-start);
                recvMess(temp);
            }
            start=index+3;
        }
    });
}

void MainWindow::recvMess(QByteArray buf)
{
    char *data=buf.data();
    ui->info->appendPlainText(tr(data));

    if(cmp_str(data,"ID:"))
    {
        ui->sInputDev->addItem(tr(&data[3]));
    }
    else if(cmp_str(data,"OD:"))
    {
        ui->sInputDev->addItem(tr(&data[3]));
    }
}


void MainWindow::resizeEvent(QResizeEvent *event)
{
    resizeUi.resizes(this->width(),this->height());
}

void MainWindow::on_pushButton_clicked()
{
    QHostAddress test;
    src_ip=ui->input->text();
    if (test.setAddress(src_ip))
    {
        tcpSocket->connectToHost(QHostAddress(src_ip),TCPPORT);
        if(tcpSocket->isOpen())
        {
            ui->info->appendPlainText(tr("connect success!"));
            setVisibles(true);
        }
        else
        {
            ui->info->appendPlainText(tr("connect failed!"));
        }
    }
    else
        ui->info->appendPlainText(tr("ip address is wrong!"));
}

void MainWindow::on_pushButton_2_clicked()
{
    if(!isRecving)
    {
        int quality=ui->soundQua->currentIndex();
        int dev=ui->sInputDev->currentIndex();

        char temp[100];
        sprintf(temp,"Rec_%d_%d_%d",dev,quality,serverSendDelay);
        tcpSocket->write(temp,20);

        clientRecv.init(quality,10025);
        clientRecv.start();

        isRecving=true;
        ui->pushButton_2->setText(tr("Stop"));
    }
    else
    {
        char temp[100];
        sprintf(temp,"StopRec");
        tcpSocket->write(temp,20);

        isRecving=false;
        ui->pushButton_2->setText(tr("Recv"));
        clientRecv.stop();
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    sendUi.init(tcpSocket,clientSend,soundServer,src_ip);
    sendUi.show();
}

void MainWindow::on_pushButton_4_clicked()
{
    ui->info->appendPlainText(tr("recvBuf:")+QString::number(clientRecv.test(1)));
}

void MainWindow::on_pushButton_5_clicked()
{
    ui->info->appendPlainText(tr("recvBuf:")+QString::number(clientRecv.test(-1)));
}

void MainWindow::on_pushButton_7_clicked()
{
    serverSendDelay++;
    ui->info->appendPlainText(tr("send delay:")+QString::number(serverSendDelay));
}

void MainWindow::on_pushButton_6_clicked()
{
    serverSendDelay--;
    if(serverSendDelay<1)
        serverSendDelay=1;
    ui->info->appendPlainText(tr("send delay:")+QString::number(serverSendDelay));
}
